package com.scoy.honeymei.activity.mall;

import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.KeyEvent;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.viewpager.widget.ViewPager;

import com.google.android.material.tabs.TabLayout;
import com.scoy.honeymei.R;
import com.scoy.honeymei.adapter.tabadapter.TabFragmentAdapter;
import com.scoy.honeymei.base.BaseActivity;
import com.scoy.honeymei.fragment.home.FilmFragment;
import com.scoy.honeymei.fragment.home.TravelSearchFragment;
import com.scoy.honeymei.fragment.home.TripFragment;
import com.scoy.honeymei.utils.MyUtil;
import com.scoy.honeymei.utils.NoScrollViewPager;

import java.util.ArrayList;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * @ClassName: SearchGoodsActivity$
 * @Description: 首页搜索
 * @Author: GaoZhan
 * @CreateDate: 2019/9/11/0011$ 下午 15:20$
 * @UpdateUser: 更新者
 * @UpdateDate: 2019/9/11/0011$ 下午 15:20$
 * @UpdateRemark: 更新内容
 */
public class SearchHomeActivity extends BaseActivity {


    @BindView(R.id.title_llt)
    LinearLayout titleLlt;
    @BindView(R.id.back_iv)
    ImageView backIv;
    @BindView(R.id.hm_search_et)
    EditText hmSearchEt;
    @BindView(R.id.hm_cancle_tv)
    TextView hmCancleTv;
    @BindView(R.id.hm_tab)
    TabLayout hmTab;
    @BindView(R.id.hm_vp)
    NoScrollViewPager hmVp;
    @BindView(R.id.nodata_tv)
    TextView nodataTv;
    private SearchHomeActivity mContext;
    private String[] mTitles = new String[]{"电影", "景点", "出行"};
    private ArrayList<Fragment> fragments;
    private TabFragmentAdapter tabFragmentAdapter;
    private int stateInt;
    private int type;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_search_home);
        ButterKnife.bind(this);
        mContext = SearchHomeActivity.this;
        type=getIntent().getIntExtra("type",0);
        if(type==1){
            hmTab.setVisibility(View.GONE);
            hmVp.setNoScroll(true);
            hmSearchEt.setHint("请输入搜索内容");
        }
        initNormal();

    }

    @Override
    public void initNormal() {

        fragments = new ArrayList<>();

        MyUtil.setStatusBar(mContext, getWindow(), true, R.color.colorWhite);
        titleLlt.setPadding(0, MyUtil.getStatusBarHeight(mContext), 0, 0);

        hmSearchEt.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
                if (TextUtils.isEmpty(hmSearchEt.getText())) {
                    return false;
                }
                if (actionId == EditorInfo.IME_ACTION_DONE) {
                    nodataTv.setVisibility(View.GONE);
                    initTab();
                }
                return false;
            }
        });

        hmSearchEt.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
//                nodataTv.setVisibility(View.VISIBLE);

                if (TextUtils.isEmpty(hmSearchEt.getText())) {
                    stateInt = 0;
                    hmCancleTv.setText("取消");

                } else {
                    if (stateInt != 1) {
                        stateInt = 1;
                        hmCancleTv.setText("搜索");
                    }
                }
            }
        });

    }

    private void initTab() {
        if (fragments.size() > 0) {
            fragments.clear();
        }
        fragments.add(FilmFragment.newInstance(1));
        fragments.add(TravelSearchFragment.newInstance(2));
        fragments.add(TripFragment.newInstance(3));
        tabFragmentAdapter = new TabFragmentAdapter(getSupportFragmentManager(), fragments, mTitles);
        hmTab.setupWithViewPager(hmVp, true);
        hmVp.setAdapter(tabFragmentAdapter);
        hmVp.setCurrentItem(type);

    }

    public String getSearchStr() {
        return hmSearchEt.getText().toString();
    }


    @OnClick({R.id.back_iv, R.id.hm_cancle_tv})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.back_iv:
                finish();
                break;
            case R.id.hm_cancle_tv:
                if (stateInt == 1) {
                    nodataTv.setVisibility(View.GONE);
                    initTab();
                } else {
                    hmSearchEt.setText("");
                    nodataTv.setVisibility(View.VISIBLE);
                    finish();
                }

                break;
        }
    }
}
